package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.RecruitmentDTO;
import cn.xn.hrms.api.dto.req.RecruitmentReqDTO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.RecruitmentService;
import cn.xn.hrms.infrastructure.dao.RecruitmentDao;

/**
 * @author ysz
 */
@Service
public class RecruitmentServiceImpl implements RecruitmentService {

    @Resource
    RecruitmentDao recruitmentDao;

    @Override
    public PageInfo<RecruitmentDTO> findAll(Integer departmentId, int recruitStatus, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (ObjectUtils.isEmpty(departmentId)) {
            departmentId = null;
        }
        List<RecruitmentDTO> personalSalaries = recruitmentDao.selectAll(departmentId, recruitStatus);
        return new PageInfo<>(personalSalaries);
    }
    @Override
    public List<RecruitmentDTO> all() {
        return recruitmentDao.selectAll(null, 0);
    }

    @Override
    public void insert(RecruitmentReqDTO recruitmentReqDTO) {
        RecruitmentDTO recruitmentDTO = new RecruitmentDTO();
        BeanUtils.copyProperties(recruitmentReqDTO, recruitmentDTO);
        recruitmentDao.insertSelective(recruitmentDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        recruitmentDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, RecruitmentReqDTO recruitmentReqDTO) {
        RecruitmentDTO recruitmentDTO = recruitmentDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(recruitmentDTO)) {
            throw new BaseException(ResultEnum.RECRUIT_NOT_EXIST);
        }
        BeanUtils.copyProperties(recruitmentReqDTO, recruitmentDTO);
        recruitmentDao.updateByPrimaryKeySelective(recruitmentDTO);
    }
}
